package jgl.vajra.management.widget.loopviewpage;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import jgl.vajra.management.R;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.manager.UMAnalysisManager;
import jgl.vajra.management.widget.bean.BannerInfo;
import jgl.vajra.management.widget.giide.GlideImageLoader;
import jgl.vajra.management.widget.mvvm.view.AppActivityManager;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.fragment.app.FragmentActivity;


/**
 * @author ryze
 * @since 1.0  2016/07/17
 */
public class AutoSwitchAdapter extends AutoLoopSwitchBaseAdapter {

    private WeakReference<Context> mContext;

    private List<BannerInfo> mDatas;

    public AutoSwitchAdapter() {
        super();
    }

    public AutoSwitchAdapter(Context mContext, List<BannerInfo> mDatas) {
        this.mContext = new WeakReference<>(mContext);
        this.mDatas = mDatas;
    }

    @Override
    public int getDataCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public View getView(int position) {
       View view = View.inflate(mContext.get(), R.layout.item_home_banner, null);
        ImageView imageView = view.findViewById(R.id.iv_image);
        /* ImageView imageView = new ImageView(mContext.get());
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
        */
        final BannerInfo info = (BannerInfo) getItem(position);
        // 展示数据
        GlideImageLoader.onBannerDisplayImage(imageView, info.coverImage);
        // GlideImageLoader.onDisplayRadius(imageView, info.pic_url,R.dimen.dp_16);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> map = new HashMap<>();
                map.put("linkValue", info.linkValue);
                map.put("position", position);
                UMAnalysisManager.sendHomeBannerClick(AppActivityManager.getAppActivityManager().currentActivity(), map);
                if (info.linkType==20){
                    IntentManager.toCourseDetailsActivity((FragmentActivity)mContext.get(),info.linkValue);
                }else if (info.linkType==10){
                    IntentManager.toBaseWebViewActivity(mContext.get(),info.linkValue);
                }
            }
        });
        return view;
    }


    @Override
    public Object getItem(int position) {
        if (position >= 0 && position < getDataCount()) {
            return mDatas.get(position);
        }
        return null;
    }


    @Override
    public View getEmptyView() {
        return null;
    }

    @Override
    public void updateView(View view, int position) {

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }
}
